<template>
  <div>
    <BHeader></BHeader>
    <div class="cart">
      <div class="step">
        <el-steps :active="active" finish-status="success" simple>
          <el-step title="我的购物车" icon="el-icon-shopping-cart-2"></el-step>
          <el-step title="确认订单" icon="el-icon-receiving"></el-step>
          <el-step title="完成订单" icon="el-icon-document-checked"></el-step>
        </el-steps>
      </div>
      <el-divider></el-divider>
      <div class="goodslist">
        <div class="tablelist">
          <el-table
            ref="multipleTable"
            border
            :data="tableData"
            tooltip-effect="dark"
            style="width: 100%"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="60"></el-table-column>
            <el-table-column label="商品" width="300" align="center">
              <template slot-scope="scope">
                <img :src="'http://124.223.89.139/pic/'+scope.row.img" width="80" height="60" />
                <!-- <router-link
                :to="'/detail/'+scope.row.id"
                style="text-decoration:none"
              ></router-link> -->
                {{ scope.row.bname }}
              </template>
            </el-table-column>
            <el-table-column prop="bid" v-show="false"></el-table-column>
            <el-table-column label="单价" width="160" align="center">
                  <template slot-scope="scopeA">
                    <span v-if="scopeA.row.currprice!=null">
                      {{scopeA.row.currprice}}
                    </span>
                     <span v-if="scopeA.row.currprice==null">
                      {{scopeA.row.oriprice}}
                    </span>
              </template>
            </el-table-column>
            <el-table-column label="数量" width="200" align="center">
              <template slot-scope="scope">
                <el-input-number
                  size="mini"
                  v-model="scope.row.number"
                  @change="handleChange(scope.row)"
                  :min="1"
                  :max="999"
                ></el-input-number>
              </template>
            </el-table-column>
            <el-table-column label="单品总价" width="200">
              <template slot-scope="scopeC" >
                <span v-if="scopeC.row.currprice!=null">{{scopeC.row.number * scopeC.row.currprice}}</span>
                <span v-if="scopeC.row.currprice==null">{{scopeC.row.number * scopeC.row.oriprice}}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  @click="handleDelete(scope.row.id)"
                  type="text"
                  size="small"
                  >移除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>

        <div style="float:right;margin-top:50px">
          <router-link to="/"
            ><el-button type="info">继续购物</el-button></router-link
          >
          <el-button type="danger" @click="settlement">结算</el-button>
        </div>
      </div>
    </div>
  </div>
</template>
<style scoped>
.cart {
  margin: 0 auto;
  width: 1200px;
  height: 800px;
}
.step {
  margin: 0 auto;
  width: 1200px;
  height: 80px;
}
.goodslist {
  margin: 0 auto;
  width: 1200px;
  height: 600px;
}
.total {
  margin: 0 auto;
  width: 1200px;
  height: 50px;
  background-color: floralwhite;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
</style>

<script>
import BHeader from "../../components/BHeader.vue";
import { getCarListByUid, doDelete, editCount } from "../../api/car/car";
export default {
  name: "Car",
  components: {
    BHeader,
  },
  data() {
    return {
      subtotal: "",
      active: 0,
      tableData: [],
      multipleTable: [],
      orderinfo:[]
    };
  },
  methods: {
    getCarList() {
      getCarListByUid().then((res) => {
        if (res.flag) {
          this.tableData = res.data;
        }
      });
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleTable = val;
    },
    next() {
      if (this.active++ > 2) this.active = 0;
    },
    handleChange(row) {
      editCount(row.id, row.number).then((res) => {
        if (res.flag) {
          this.getCarList();
        }
      });
    },
    handleDelete(id) {
      doDelete(id).then((res) => {
        if (res.flag) {
          this.getCarList();
        }
      });
    },
    settlement() {
    for(let i=0;i<this.multipleTable.length;i++){
      this.orderinfo.push(this.multipleTable[i])
    }
      localStorage.removeItem("orderinfo")
     localStorage.removeItem("cartOrder")
      localStorage.setItem("cartOrder",JSON.stringify(this.orderinfo))
      this.$router.push("/beforecreorder");
    },
  },
  created() {
    this.getCarList();
  },
};
</script>
